package frame.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import frame.common.ConstantEntity;
import frame.exception.FrameException;

/**
 * 这个类相当于try{}catch(){}的catch部分。只在有异常发生时resolveException
 * 方法才被执行。这个类要做的事情为：1，将异常信息写到日志文件。2，识别某些异常信息，生成
 * 相应的ModelAndView返回。
 * 
 * @author MR.CUIPENG
 *
 */
public class FrameExceptionHandler implements HandlerExceptionResolver{

	private Logger logger = Logger.getLogger(getClass());
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception exp) {

		
		if (exp instanceof FrameException){
			logger.error("有异常被拦截到", exp);
		}
		//exp.printStackTrace();TODO 返回json形式的数据
		if(request.getHeader("X-Requested-With")==null){//非异步请求
			return new ModelAndView("frame/errorPage");
		}else{//异步请求
//			MappingJacksonJsonView view=new MappingJacksonJsonView();
			ModelAndView view=new ModelAndView();
			view.addObject("000001", ConstantEntity.MESSAGE_TYPE_ERROR);
			return view;
		}
	}

}
